package com.changan.controller;

import com.changan.error.BusinessException;
import com.changan.error.EmBusinessError;
import com.changan.response.CommonReturnType;
import io.jsonwebtoken.SignatureException;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName BaseController
 * @Description 异常处理的基类
 * @Version 1.0
 **/
public class BaseController {
    @ExceptionHandler(Exception.class)
    //@ResponseBody
    //@ResponseStatus(HttpStatus.OK)
    public Object handlerException(HttpServletRequest request, Exception e){
        Map<String,Object> responseData = new HashMap<>();
        if(e instanceof BusinessException){
            BusinessException businessException = (BusinessException)e;

            responseData.put("errCode",businessException.getErrCode());
            responseData.put("errMsg",businessException.getErrMsg());
        }else if(e instanceof SignatureException){
            responseData.put("errCode", EmBusinessError.AUTHORIZATION_EXCEPTION.getErrCode());
            responseData.put("errMsg",EmBusinessError.AUTHORIZATION_EXCEPTION.getErrMsg());
        }else{
            responseData.put("errCode", EmBusinessError.UNKNOWN_ERROR.getErrCode());
            responseData.put("errMsg",EmBusinessError.UNKNOWN_ERROR.getErrMsg());
        }
        return CommonReturnType.create("fail",responseData);
    }
}
